package cc.fozone.note.view;

import javafx.beans.value.ChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import cc.fozone.note.beans.Article;
import cc.fozone.note.configuration.IConfiguration;

public class EditorView extends BorderPane {
	private IConfiguration<String,String> configuration;
	
	private TextArea input;
	private BorderPane info;
	private TextField infoTitle;
	private Button infoSave;
	private Button infoRemove;
	private FlowPane infoCategory;
	private Label infoCategoryTitle;
	private Hyperlink infoCategoryChoose;
	
	private double scrollHeight = 0;
	//private double scrollWidth = 0;
	
	private Article article;
	private String html;
	
	private String styleClass = "editor";
	private String inputStyleClass = "editor-input";
	private String infoStyleClass = "editor-info";
	private String infoTitleStyleClass = "info-title";
	private String infoTitlePlaceholder = "文本标题";
	
	private String infoSaveStyleClass = "info-save";
	private String infoSaveLabel = "保存";
	private String infoRemoveLabel="删除";
	private String infoRemoveStyleClass = "info-remove";
	
	private String infoCategoryStyleClass = "info-category";
	private String infoCategoryLabel = "分类";
	private String infoCategoryLabelStyleClass = "info-category-label";
	private String infoCategoryChooseStyleClass = "info-category-choose";
	private String infoCategoryChooseLabel = "默认";
	
	public EditorView(IConfiguration<String, String> configuration) {
		// TODO Auto-generated constructor stub
		this.configuration = configuration;
		this.article = new Article();
		this.init();
	}
	
	private void init() {
		this.buildConfiguration();
		this.buildView();
		this.buildLayout();
	}

	private void buildConfiguration() {
		// TODO Auto-generated method stub
		styleClass = configuration.get("app.content.editor.styleClass",styleClass);
		inputStyleClass = configuration.get("app.content.editor.inputStyleClass",inputStyleClass);
		infoStyleClass = configuration.get("app.content.editor.infoStyleClass",infoStyleClass);
		
		infoTitlePlaceholder = configuration.get("app.content.editor.info.titlePlaceholder",infoTitlePlaceholder);
		infoTitleStyleClass = configuration.get("app.content.editor.info.titleStyleClass",infoTitleStyleClass);
		
		infoSaveStyleClass = configuration.get("app.content.editor.info.titleSaveClass",infoSaveStyleClass);
		infoSaveLabel = configuration.get("app.content.editor.info.titleSaveLabel",infoSaveLabel);
		infoRemoveStyleClass = configuration.get("app.content.editor.info.titleRemoveClass",infoRemoveStyleClass);
		infoRemoveLabel = configuration.get("app.content.editor.info.titleRemoveLabel",infoRemoveLabel);
		
		infoCategoryStyleClass = configuration.get("app.content.editor.info.categoryStyleClass",infoCategoryStyleClass);
		infoCategoryLabel = configuration.get("app.content.editor.info.categoryLabel",infoCategoryLabel);
		infoCategoryLabelStyleClass = configuration.get("app.content.editor.info.categoryLabelStyleClass",infoCategoryLabelStyleClass);
		infoCategoryChooseStyleClass = configuration.get("app.content.editor.info.categoryChooseStyleClass",infoCategoryChooseStyleClass);
		infoCategoryChooseLabel = configuration.get("app.content.editor.info.categoryChooseLabel",infoCategoryChooseLabel);
	}

	private void buildView() {
		// TODO Auto-generated method stub
		input = new TextArea();
		input.getStyleClass().add(inputStyleClass);
		this.setCenter(input);
		
		info = new BorderPane();
		info.getStyleClass().add(infoStyleClass);
		
		infoTitle = new TextField();
		infoTitle.setPromptText(infoTitlePlaceholder);
		infoTitle.getStyleClass().add(infoTitleStyleClass);
		
		info.setTop(infoTitle);
		
		HBox box = new HBox();
		infoSave = new Button(infoSaveLabel);
		infoSave.getStyleClass().add(infoSaveStyleClass);
		
		infoRemove = new Button(infoRemoveLabel);
		infoRemove.getStyleClass().add(infoRemoveStyleClass);
		
		box.getChildren().addAll(infoRemove,infoSave);
		
		info.setRight(box);
		
		infoCategory = new FlowPane();
		infoCategory.getStyleClass().add(infoCategoryStyleClass);
		info.setCenter(infoCategory);
		
		infoCategoryTitle = new Label(infoCategoryLabel);
		infoCategoryTitle.getStyleClass().add(infoCategoryLabelStyleClass);
		
		infoCategoryChoose = new Hyperlink(infoCategoryChooseLabel);
		infoCategoryChoose.getStyleClass().add(infoCategoryChooseStyleClass);
		
		infoCategory.getChildren().addAll(infoCategoryTitle,infoCategoryChoose);
		
		this.setTop(info);
		
		this.getStyleClass().add(styleClass);
		
	}
	
	private void buildLayout() {
		// TODO Auto-generated method stub
	}
	
	public void setText(String text){
		input.setText(text);
	}
	
	public String getText(){
		return input.getText();
	}
	
	public void setTitle(String value){
		infoTitle.setText(value);
	}
	
	public String getTitle(){
		return infoTitle.getText();
	}
	
	public void setArticle(Article article){
		this.article = article;
	}
	
	public Article getArticle(){
		return this.article;
	}
	
	public void setArticleId(long id){
		this.article.setId(id);
	}
	
	public void setHTML(String html){
		this.html = html;
	}
	
	public String getHTML(){
		return this.html;
	}
	
	public void updateArticle() {
		// TODO Auto-generated method stub
		article.setTitle(this.getTitle());
		article.setMarkdown(this.getText());
		article.setContent(html);
	}

	public void updateView() {
		// TODO Auto-generated method stub
		if(article == null) {
			article = new Article();
		}
		String text = article.getMarkdown();
		this.setText(text == null ? "":text);
		
		String title = article.getTitle();
		this.setTitle(title==null?"":title);
	}
	
	public void resetArticle(){
		article = new Article();
	}

	public double getScrollTop(){
		return input.getScrollTop();
	}
	
	public void setScrollTop(double value){
		input.setScrollTop(value);
	}
	
	public double getScrollLeft(){
		return input.getScrollLeft();
	}
	
	/**
	 * 更新可视高度
	 */
	public void updateScrollHeight() {
		double height = -1;
		
		ObservableList<Node> list = input.getChildrenUnmodifiable();
		
		if(list.isEmpty()) height = input.getHeight();
		for(Node node : list){
			if(node instanceof ScrollPane) {
				ScrollPane pane = (ScrollPane) node;
				Region content = (Region) pane.getContent();
				height = content.getHeight();
				
				/*
				ObservableList<Node> sublist = pane.getChildrenUnmodifiable();
				for(Node subNode : sublist){
					if(subNode instanceof ScrollBar) {
						ScrollBar bar = (ScrollBar) subNode;
						if(bar.getOrientation() == Orientation.VERTICAL) {
							height = ScrollPaneHelper.getBlankValue(bar);
							//height = bar.getVisibleAmount();
							break;
						}
					}
				}
				break;
				*/
			}
		}
		this.scrollHeight = height;
	}
	
	public double getScrollHeight(){
		return scrollHeight;
	}

	public void setOnTextScroll(ChangeListener<Number> listener){
		input.scrollTopProperty().addListener(listener);
	}
	
	public void setOnEditorKeyReleased(EventHandler<KeyEvent> eventHandler) {
		// TODO Auto-generated method stub
		input.setOnKeyPressed(eventHandler);
	}
	
	public void setOnTitleKeyReleased(EventHandler<KeyEvent> eventHandler) {
		// TODO Auto-generated method stub
		infoTitle.setOnKeyReleased(eventHandler);
	}
	
	public void setOnSaveAction(EventHandler<ActionEvent> eventHandler){
		infoSave.setOnAction(eventHandler);
	}
	
	public void setOnRemoveAction(EventHandler<ActionEvent> eventHandler){
		infoRemove.setOnAction(eventHandler);
	}
	
	
}
